package com.funo.business.ctrler;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.funo.business.entities.TemplateItem;
import com.funo.business.service.TemplateService;
import com.funo.common.factory.JsonMsgFactory;
import com.funo.common.pojo.PageResult;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.utils.Constant;
import com.funo.core.base.BaseCtrler;
import com.funo.main.entities.User;

@Controller
@RequestMapping(value="/template")
public class TemplateCtrler extends BaseCtrler{
	
	@Autowired
	@Qualifier("templateService")
	private TemplateService templateService;
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public String add(TemplateItem templateItem ){
		if (templateItem!=null) {
			try {
				Integer type = templateService.saveTemplate(templateItem);
				if (type == Constant.STATUS_SUSSESS) {//成功
					return toJson(JsonMsgFactory.successMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
				return toJson(JsonMsgFactory.failMsg());
			}
		}
		return null;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public String edit(TemplateItem templateItem){
		try {
			Integer type = templateService.updateTemplate(templateItem);
			if (type == Constant.STATUS_SUSSESS) {//成功
				return toJson(JsonMsgFactory.successMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
		return null;
	}
	
	@RequestMapping(value = "/page")
	public @ResponseBody String page(QueryCondition condition,TemplateItem item) {
		PageResult<TemplateItem> page = templateService.getTemplatePage(condition,item);
		return toJson(page);
	}
	
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public @ResponseBody String del(String deleteIds){
		try {

			String[]Ids=deleteIds.split(",");//传递到数据库的参数

			for (int i = 0; i < Ids.length; i++) {
				String id=Ids[i];
				templateService.delTemplate(id);
			}
			return toJson(JsonMsgFactory.successMsg());
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
	}
	
	@RequestMapping(value="/saveTempalteByCompany",method=RequestMethod.POST)
	public @ResponseBody String saveTempalteByCompany(HttpSession session,String templateId,String companyId){
		System.out.println(templateId);
		System.out.println(companyId);
		User user = sessionUser(session);
		if(user == null || user.getId() == null){//丢失session
			return toJson(JsonMsgFactory.fail("请先登录,再进行操作!"));
		}
		try {
			Integer type = templateService.saveTempalteByCompany(companyId, templateId);
			if (type == Constant.STATUS_SUSSESS) {//成功
				return toJson(JsonMsgFactory.successMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
		return null;
	}
	
	@Override
	protected String handlerException(Exception e) {
		e.printStackTrace();
		System.err.println("[handlerException:MemberController]异常类型："+e.getClass().getSimpleName());
		System.err.println(e.getMessage());
		return "系统异常";
	}
}
